package cn.nfwx.tv.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.nfwx.tv.R;
import cn.nfwx.tv.model.KeywordModel;


/**
 * 热门搜索
 * Created by zb.liu by  2017/11/2.
 */
public class SearchHotAdapter extends BaseAdapter {
    private Context mContext;
    private List<Object> keywords = new ArrayList<>();
    //private String[] titles = new String[]{"雷神：3", "复仇者联盟3", "钢铁侠4", "钢铁侠4", "钢铁侠4", "钢铁侠4", "钢铁侠4", "钢铁侠4"};
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void itemClick(View view, int position, String keyword);
    }

    public SearchHotAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return keywords.size();
    }

    @Override
    public Object getItem(int position) {
        return keywords.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.activity_search_hot_item, null);
            holder = new ViewHolder();
            holder.tv_name = (TextView) convertView.findViewById(R.id.search_tvprogram_name);
            holder.tv_idnex = (TextView) convertView.findViewById(R.id.search_tvprogram_index);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final Object object = keywords.get(position);
        if (object instanceof KeywordModel) {
            KeywordModel item = (KeywordModel) object;
            holder.tv_name.setText(item.getKeyword());
        } else if (object instanceof String) {
            String str = (String) object;
            holder.tv_name.setText(str);
        }
        holder.tv_idnex.setText(position + 1 + "");
        if (position == 0) {
            holder.tv_idnex.setBackgroundResource(R.drawable.search_index1_btn);
        } else if (position == 1) {
            holder.tv_idnex.setBackgroundResource(R.drawable.search_index2_btn);
        } else if (position == 2) {
            holder.tv_idnex.setBackgroundResource(R.drawable.search_index3_btn);
        } else {
            holder.tv_idnex.setBackgroundResource(R.drawable.search_index_btn);
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    if (object instanceof KeywordModel) {
                        KeywordModel item = (KeywordModel) object;
                        onItemClickListener.itemClick(v, position, item.getKeyword());
                    } else if (object instanceof String) {
                        String str = (String) object;
                        onItemClickListener.itemClick(v, position, str);
                    }
                }
            }
        });

        return convertView;
    }


    public void setNewData(List newData) {
        keywords.clear();
        keywords.addAll(newData);
        notifyDataSetChanged();
    }

    static class ViewHolder {
        private TextView tv_name;
        private TextView tv_idnex;
    }
}
